define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'vbot/variable/index' + location.search,
                    add_url: 'vbot/variable/add',
                    edit_url: 'vbot/variable/edit',
                    del_url: 'vbot/variable/del',
                    multi_url: 'vbot/variable/multi',
                    view_variable_url:'vbot/variable/view_variable',
                    table: 'vbot_variable',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {
                            field: 'value_source',
                            title: __('Value_source'),
                            searchList: {"0": __('Value_source 0'), "1": __('Value_source 1')},
                            formatter: Table.api.formatter.normal
                        },
                        {field: 'sql', title: __('Sql'), formatter: Controller.api.formatter.sql_formatter},
                        {field: 'namespace', title: __('Namespace')},
                        {field: 'class', title: __('Class')},
                        {field: 'function', title: __('Function')},
                        {field: 'params', title: __('Params')},
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name:'view variable',
                                    title:'查看变量值',
                                    classname:'btn btn-xs btn-info btn-ajax',
                                    icon:'fa fa-magic',
                                    url:$.fn.bootstrapTable.defaults.extend.view_variable_url,
                                    confirm: '确定要计算并查看变量的当前值吗？',
                                    success: function (data, ret) {
                                        Layer.alert(data.variable_value,{
                                            'title':'计算结果'
                                        });
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'vbot/variable/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'vbot/variable/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'vbot/variable/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                function need_show(value) {
                    if (value == 1) {
                        $('.variable_function').show(200);
                        $('.variable_sql').hide();
                    } else {
                        $('.variable_sql').show(200);
                        $('.variable_function').hide();
                    }
                }

                need_show($('#c-value_source')[0].value);

                $('#c-name').on('change', function () {
                    $('#reference-name').html('可在通知模板中引用，引用名称：${' + this.value + '}');
                })

                $('#c-value_source').on('change', function (argument) {
                    need_show(this.value);
                })
            },
            formatter: {
                sql_formatter: function (value, row) {
                    if (value.length > 30) {
                        return value.substring(0, 30) + '...';
                    }

                    return value;
                }
            }
        }
    };
    return Controller;
});